package com.hxc.shop.utils;

import com.hxc.shop.common.ResultCode;
import com.hxc.shop.common.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class PoiUtil {


    /**
     * @param file 文件
     * @Description 读取excel
     * */
    public ResultVO ReadExcel(MultipartFile file) {
        // 读取文件
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        }catch (Exception e) {
            return new ResultVO(ResultCode.ERROR.getCode(),"文件读取失败!"+e.getMessage());
        }
        // 创建excel实例
        Workbook workbook = null;
        // 判断文件格式是否为xlsx还是xls
        if (file.getOriginalFilename().matches("^.+\\.(?i)(xlsx)$")) {
            try {
                workbook = new XSSFWorkbook(inputStream);
            }catch (Exception e) {
                return new ResultVO(ResultCode.ERROR.getCode(),"读取xlsx格式的excel文件失败:"+e.getMessage());
            }
        } else if (file.getOriginalFilename().matches("^.+\\.(?i)(xls)$")){
            try{
                workbook = new HSSFWorkbook(inputStream);
            }catch (Exception e) {
                return new ResultVO(ResultCode.ERROR.getCode(),"读取xls格式的excel文件失败:"+e.getMessage());
            }
        } else {
            return new ResultVO(ResultCode.ERROR.getCode(),"该文件不是excel文件");
        }
        // 定义变量接收excel的值,按照sheet，行数，每行数据进行读取赋值
        Map<String,Map<String,List<String>>> dataList = new HashMap<>();
        // 获取excel的sheet数
        Integer SheetLength = workbook.getNumberOfSheets();
        for (int sheetCount = 0; sheetCount<SheetLength;sheetCount++) {
            // sheet名称
            String sheetName = "sheet"+sheetCount;

            Sheet sheet = workbook.getSheetAt(sheetCount);
            if (sheet == null) {
                log.error("该excel文件第"+sheetCount+1+"个Sheet无数据");
                continue;
            }
            // 获取列数
            int column = sheet.getRow(0).getPhysicalNumberOfCells();
            if (column == 0) {
                return new ResultVO(ResultCode.ERROR.getCode(),"该excel文件没有数据!列数为空!");
            }
            // 获取行数
            int rows = sheet.getRow(0).getPhysicalNumberOfCells();
            if (rows == 0) {
                return new ResultVO(ResultCode.ERROR.getCode(),"该excel文件没有数据!行数为空!");
            }
            Map<String,List<String>> rowsMapData =new HashMap<>();
            // 遍历Excel
            for (int rowCount = 1; rowCount<=sheet.getLastRowNum();rowCount++) {
                String rowName = "row"+rowCount;
                List<String> rowList = new ArrayList<>();
                Row row = sheet.getRow(rowCount);
                // 判断当前行是否为空,为空读取下一行
                if (row == null) {
                   continue;
                }
                for (int columnCount = 0;columnCount<column;columnCount++) {
                    // 判断当前行的第xxx列是否有值,有值赋值，无值赋值空
                    row.getCell(columnCount).setCellType(Cell.CELL_TYPE_STRING);
                    if (StringUtils.isEmpty(row.getCell(columnCount).getStringCellValue())) {
                        rowList.add("");
                    } else {
                        rowList.add(row.getCell(columnCount).getStringCellValue());
                    }
                }
                rowsMapData.put(rowName,rowList);
            }
            dataList.put(sheetName,rowsMapData);
        }
        return new ResultVO(dataList);
    }



















}
